//
//  TencentWeibo.m
//  NewsReader
//
//  Created by blacksun on 11-3-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TencentWeibo.h"

@implementation TencentWeibo

-(id)init{
	if (self=[super init]) {
		QOauthKey *temp = [[QOauthKey alloc] init];
		temp.consumerKey = @"f21db0b1b04a4a75bd4cd96c5a416f5a";
		temp.consumerSecret = @"5f913d86d4dfac72a72c9505af6aa4f6";
		temp.callbackUrl = @"http://www.qq.com";
		temp.tokenKey = [[NSUserDefaults standardUserDefaults] stringForKey:kQQ_Token_Key];
		temp.tokenSecret = [[NSUserDefaults standardUserDefaults] stringForKey:kQQ_Token_Secret];
		self.oauthKey = temp;
		[temp release];
        
        
        self.userId = [[NSUserDefaults standardUserDefaults] stringForKey:kQQ_User_Id];
        self.userName = [[NSUserDefaults standardUserDefaults] stringForKey:kQQ_User_Name];
        
		return self;
	}
	return nil;
}

-(void)getRequestToken{
	
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	
	QOauthKey *temp = [[QOauthKey alloc] init];
	
	temp.consumerKey	= self.oauthKey.consumerKey;
	temp.consumerSecret = self.oauthKey.consumerSecret;
	temp.callbackUrl = @"http://www.qq.com";
	
	
	NSString *responseStr = [request syncRequestWithUrl:kQQ_RequestToken_Url httpMethod:@"GET" oauthKey:temp parameters:nil files:nil];
	
	[self parseTokenKeyWithResponse:responseStr];//从response中获取request token 和 token secret
	
	[request release];
	[temp release];
}

-(NSURLRequest *)getAuthorizaRequest{
	NSString *urlStr = [[NSString alloc] initWithFormat:@"%@?oauth_token=%@",kQQ_Authorize_Url,self.oauthKey.tokenKey];
	NSURL *url = [[NSURL alloc] initWithString:urlStr];
	[urlStr release];
	NSURLRequest *request = [NSURLRequest requestWithURL:url];
	[url release];
	return request;
}

-(void)getAccessToken{
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	QOauthKey *temp = [[QOauthKey alloc] init];
	
	temp.consumerKey	= self.oauthKey.consumerKey;
	temp.consumerSecret = self.oauthKey.consumerSecret;
	temp.tokenKey = self.oauthKey.tokenKey;
	temp.tokenSecret= self.oauthKey.tokenSecret;
	temp.verify = self.oauthKey.verify;
	
	NSString *retString = [request syncRequestWithUrl:kQQ_AccessToken_Url httpMethod:@"GET" oauthKey:temp parameters:nil files:nil];
	[self parseTokenKeyWithResponse:retString];
    self.userId = [self valueForKey:kQQ_User_Id_Key ofQuery:retString];
    
	[temp release];
	[request release];
	
}

-(void) getUserName{
    QWeiboRequest *request = [[QWeiboRequest alloc] init];
    NSString *showUserURL = [[NSString alloc] initWithFormat:@"%@user/info?format=json",kQQ_BaseUrl];
    NSString *retString = [request syncRequestWithUrl:showUserURL httpMethod:@"GET" oauthKey:oauthKey parameters:nil files:nil];
	
    NSDictionary *dict = [retString JSONValue];
    NSDictionary *userData = [dict objectForKey:@"data"];
    
    self.userName = [userData objectForKey:@"name"];
    
    NSLog(@"userName = %@",self.userName);
    [request release];
    [showUserURL release];
}



-(void) saveAccessToken{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setValue:self.oauthKey.tokenKey forKey:kQQ_Token_Key];
	[userDefaults setValue:self.oauthKey.tokenSecret forKey:kQQ_Token_Secret];
    [userDefaults setValue:self.userId forKey:kQQ_User_Id];
    [userDefaults setValue:self.userName forKey:kQQ_User_Name];
    
	[userDefaults synchronize];
}

//发送微薄
-(BOOL) sendTweet:(NSString *)tweet{
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSString *url = [[NSString alloc] initWithFormat:@"%@t/add",kQQ_BaseUrl];
	NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:tweet,@"content",@"json",@"format",@"127.0.0.1",@"clientip",nil];
	[request syncRequestWithUrl:url httpMethod:@"POST" oauthKey:self.oauthKey parameters:parameters files:nil];
	[parameters release];
	[url release];
	return YES;
}

@end
